using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Darilek.SandcastleExtensions.Utilities;

namespace Darilek.SandcastleExtensions.PlugIns.Configuration
{
	public partial class ResolveReferenceConfigDlg : Form
	{
		private ResolveReferencePlugInConfiguration _settings;
		private string _configuration;
		

		public ResolveReferenceConfigDlg(string configuration)
		{
			InitializeComponent();

			_settings = ConfigurationSerializer.LoadConfiguration<ResolveReferencePlugInConfiguration>(configuration);
			_configuration = configuration;

			targetsBindingSource.DataSource = _settings.Targets;
		}

		private void ResolveReferenceConfigDlg_Load(object sender, EventArgs e)
		{
		}

		public string Configuration
		{
			get { return _configuration; }
		}

		private void okButton_Click(object sender, EventArgs e)
		{
			_configuration = ConfigurationSerializer.SaveConfiguration<ResolveReferencePlugInConfiguration>(_settings);

			this.Close();
		}

		private void addDirButton_Click(object sender, EventArgs e)
		{
			using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
			{
				folderDialog.ShowNewFolderButton = false;
				if (folderDialog.ShowDialog() == DialogResult.OK)
				{
					targetsBindingSource.Add(new TargetItem(folderDialog.SelectedPath, "*.xml", true));
					targetsBindingSource.EndEdit();
				}
			}
		}

		private void addFileButton_Click(object sender, EventArgs e)
		{
			using (OpenFileDialog fileDialog = new OpenFileDialog())
			{
				fileDialog.AddExtension = true;
				fileDialog.DefaultExt = "*.xml";
				fileDialog.Filter = "Xml files (*.xml)|*.xml";
				fileDialog.FilterIndex = 0;
				fileDialog.CheckFileExists = true;
				fileDialog.CheckPathExists = true;
				fileDialog.ValidateNames = true;

				if (fileDialog.ShowDialog() == DialogResult.OK)
				{
					foreach (string safeFileName in fileDialog.FileNames)
					{
						//System.IO.Path.
						targetsBindingSource.Add(
							new TargetItem(Path.GetDirectoryName(safeFileName), Path.GetFileName(safeFileName), false)
						);
						targetsBindingSource.EndEdit();
					}
				}
			}
		}

		private void deleteAllButton_Click(object sender, EventArgs e)
		{
			targetsBindingSource.Clear();
			targetsBindingSource.EndEdit();
		}

		private void removeItemButton_Click(object sender, EventArgs e)
		{
			if (targetsGridView.SelectedRows.Count > 0)
			{
				TargetItem item = targetsGridView.SelectedRows[0].DataBoundItem as TargetItem;
				targetsBindingSource.Remove(item);
				targetsBindingSource.EndEdit();
			}

		}
	}
}